package com.woniu.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.domain.Company;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import com.woniu.domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author zyy
 * @since 2021-03-05
 */
public interface CompanyMapper extends BaseMapper<Company> {
    //关联招聘者与公司
    @Insert("INSERT INTO t_user_company(user_id,company_id) " +
            "VALUES(#{userId},#{companyId})")
    public void addUserAndCompany(Integer userId,Integer companyId);

    //根据招聘者id查询公司
    @Select("SELECT c.* " +
            "FROM t_user_company AS uc " +
            "JOIN t_company AS c " +
            "ON uc.company_id=c.company_id " +
            "WHERE uc.user_id=#{uid}")
    public Company queryCompanyByUid(Integer uid);

    @Select("select c.*\n" +
            "from t_user u\n" +
            "join t_user_company uc \n" +
            "on u.id=uc.user_id\n" +
            "join t_company c\n" +
            "on c.company_id=uc.company_id  " +
            "where u.id=#{id}")
    public Company  getCompanyInfo(Integer id);

    //根据公司id查询所有招聘人员
    @Select("SELECT u.* " +
            "FROM t_user_company AS uc " +
            "JOIN t_user AS u " +
            "ON uc.user_id=u.id " +
            "WHERE uc.company_id=#{company_id}")
    public List<User> queryStaff(Integer compId);

    //根据公司id和用户id移除招聘者
    @Delete("DELETE FROM t_user_company WHERE user_id=#{uid} AND company_id=#{compId}")
    public void deleteStaff(Integer uid,Integer compId);

    //根据公司id修改公司的招聘人数
    @Update("UPDATE t_company SET user_count=user_count+1 WHERE company_id=#{compId}")
    public void updateUserCount(Integer compId);

}
